using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAL
{
    public class CTHoaDon_DAL
    {
        private SqlConnection cn = null;
        public  CTHoaDon_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(CTHoaDon_DTO ctHoaDon)
        {
            SqlCommand cm = new SqlCommand("tbl_CTHOADON_INSERT", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaHD", ctHoaDon.MaHD);
            cm.Parameters.AddWithValue("@MaMH", ctHoaDon.MaMH);
            cm.Parameters.AddWithValue("@SoLuong", ctHoaDon.SoLuong);
            cm.Parameters.AddWithValue("@DonGia", ctHoaDon.DonGia);
            cm.Parameters.AddWithValue("@ThanhTien", ctHoaDon.ThanhTien);
            cm.ExecuteNonQuery();
        }
        public void Update(CTHoaDon_DTO ctHoaDon)
        {
            SqlCommand cm = new SqlCommand("tbl_CTHOADON_UPDATE", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@ID", ctHoaDon.ID);
            cm.Parameters.AddWithValue("@MaHD", ctHoaDon.MaHD);
            cm.Parameters.AddWithValue("@MaMH", ctHoaDon.MaMH);
            cm.Parameters.AddWithValue("@SoLuong", ctHoaDon.SoLuong);
            cm.Parameters.AddWithValue("@DonGia", ctHoaDon.DonGia);
            cm.Parameters.AddWithValue("@ThanhTien", ctHoaDon.ThanhTien);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD,int mathang)
        {
            SqlCommand cm = new SqlCommand("tb_CTHoaDon_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@ID", iD);
            cm.ExecuteNonQuery();
        }
        public tbCTHoaDonCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbl_CTHOADON_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbCTHoaDonCollection ctHoaDonCollection = new tbCTHoaDonCollection();

            while (dr.Read())
            {
                CTHoaDon_DTO ctHoaDon = new CTHoaDon_DTO();
                ctHoaDon.ID = int.Parse(dr.GetValue(0).ToString());
                ctHoaDon.MaHD = int.Parse(dr.GetValue(1).ToString());
                ctHoaDon.MaMH = int.Parse(dr.GetValue(2).ToString());
                ctHoaDon.DonGia = float.Parse(dr.GetValue(3).ToString());
                ctHoaDon.SoLuong = float.Parse(dr.GetValue(4).ToString());
                ctHoaDon.ThanhTien = float.Parse(dr.GetValue(5).ToString());

                ctHoaDonCollection.Add(ctHoaDon);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ctHoaDonCollection;
        }
    }
}
